package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import model.Stack;

/**
 * 
 * @author Alexey Kuprin
 * 
 */
public class Notification extends JDialog {
	private JLabel label;
	private JTextArea textException;
	private JScrollPane textExceptionScroll;
	private Stack stack;

	public Notification(Stack s) {

		stack = s;
		label = new JLabel(s.toString()
				+ " is about to exceed available process time.");
		label.setSize(300, 20);
		label.setLocation(20, 20);

		JButton btn = new JButton("Show stack info");
		btn.setSize(100, 20);
		btn.setLocation(350, 20);
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				StackInfoDialog info = new StackInfoDialog(stack);
				info.setVisible(true);
				dispose();
			}
		});

		this.add(btn);
		this.add(label);

		this.setModalityType(ModalityType.APPLICATION_MODAL);
		this.setSize(500, 100);
		this.setTitle("Stack process time alert");
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setLocation(300, 150);
		this.setLayout(null);

	}

	public Notification(Exception e) {
		this(e, null);
	}

	public Notification(Exception e, String message) {
		label = new JLabel(e.getClass().getSimpleName() + ": " + e.getMessage());
		label.setSize(700, 20);
		label.setLocation(5, 0);
		this.add(label);

		textException = new JTextArea(ExceptionHandler.getStackTraceAsString(e));
		textException.setLocation(5, 25);
		textException.setSize(890, 370);
		this.add(textException);
		textExceptionScroll = new JScrollPane(textException);
		textExceptionScroll.setBounds(5, 30, 790, 365);
		this.add(textExceptionScroll);

		this.setModalityType(ModalityType.APPLICATION_MODAL);
		this.setSize(817, 439);
		this.setLocation(300, 150);
		this.setTitle(message != null ? message
				: "Application has generated a checked exception");
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setLayout(null);
	}
}